package com.renge.ucenterservice.controller;


import com.renge.commonutils.DateUtils;
import com.renge.commonutils.R;
import com.renge.exception.GuliException;
import com.renge.ucenterservice.entity.UcenterMember;
import com.renge.ucenterservice.entity.vo.LoginVo;
import com.renge.ucenterservice.entity.vo.RegisterVo;
import com.renge.ucenterservice.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-20
 */
@RestController
@Api(description="会员客户管理")
@CrossOrigin
@RequestMapping("/ucenterservice/ucenter-member")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "统计每日注册人数")
    @GetMapping("/coderegister/{day}")
    public R checkDayByCode(@ApiParam(name = "day", value = "日期格式：2021-06-29", required = true)
                                @PathVariable("day") String day){
        //判断日期格式是否符合2021-07-02
        boolean b = DateUtils.isLegalDate(day);
        if(!b){
            return R.error();
        }else {
            Integer dayCode =ucenterMemberService.checkDayByCode(day);
            return R.ok().data("dayCode",dayCode);
        }
    }

    @ApiOperation(value = "注册验证码")
    @GetMapping("/phone/{number}")
    public R phoneByCode(@ApiParam(name = "number", value = "根据手机号获得验证码", required = true)
                               @PathVariable("number")String number){
        String code =ucenterMemberService.phoneByCode(number);
        return R.ok().data("code",code);
    }

    @ApiOperation(value = "登录用户返回token")
    @PostMapping("/login")
    public R loginToToken(@ApiParam(name = "LoginVo", value = "根据登录手机号和密码进行登录", required = true)
                              @RequestBody LoginVo loginVo){
        String token= ucenterMemberService.loginToToken(loginVo);
        return R.ok().data("token",token);
    }
    @ApiOperation(value = "传入HttpServletRequest解析token")
    @GetMapping("/token")
    public R getUserByToken(@ApiParam(name = "request", value = "根据request的头部获得token", required = true)
                         HttpServletRequest request){
        UcenterMember ucenterMember=ucenterMemberService.getUserByToken(request);
        return R.ok().data("ucenterMember",ucenterMember);
    }

    @ApiOperation(value = "注册用户")
    @PostMapping("/register")
    public R register(@ApiParam(name = "RegisterVo", value = "注册", required = true)
                          @RequestBody RegisterVo registerVo){
        boolean b= ucenterMemberService.saveRegister(registerVo);
        if(!b){
            throw new GuliException(20001,"注册失败");
        }
        return R.ok();
    }


}

